// pages/goods/components/goodspicker/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    specs: {
      type: Array,
      value: 0
    },
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 图片自适应
     */
    loadpic: function(event) {
      let pindex = event.currentTarget.dataset.idx
      let index = event.currentTarget.dataset.iid
      let specs = this.data.specs
      if (!specs) {
        return false
      }
      let picitems = specs[pindex].itemList
      let picWidth = event.detail.width
      let picHeight = event.detail.height
      picitems[index].styleCss = {
        width: picWidth,
        height: picHeight
      }
      this.setData({
        specs: specs
      })
    },
    // 选规格
    specsTap: function(e) {
      this.setData({
        current: 0
      })
      // this.setData({
      //   isID: event.currentTarget.dataset.id
      // })
      var $this = this

      let index = e.currentTarget.dataset.idx
      let title = e.currentTarget.dataset.title
      let iid = e.currentTarget.dataset.iid
      // return false

      $this.data.spectitle[index][1] = title
      $this.data.specsData[index] = $this.data.specs[index].itemList[iid]

      $this.setData({
        spectitle: $this.data.spectitle,
        specsData: $this.data.specsData
      })
      $this.getgoodsSku()
    },
    //获取商品规格id
    getgoodsSku() {
      let $this = this
      let specs = []
      $this.data.spectitle.forEach(ele => {
        let eles = ele.join(":")
        specs.push(eles)
      })
      specs = specs.join(",")
      let apicontent = api.goods.getGoodsSku()
      core.post(apicontent, {
        id: $this.data.goods.id,
        specs,
      }, function(res) {
        if (res.code == 1) {

          let skubanner = res.data.banner ? JSON.parse(res.data.banner) : []

          let banner = $this.data.goods.thumbs

          let newBanner = []
          if (skubanner.length > 0) {
            skubanner.forEach(e => {
              newBanner.push(e)
            })
          }
          if (banner.length > 0) {
            banner.forEach(e => {
              newBanner.push(e)
            })
          }


          if (res.data.virtualStock <= 0) {
            $this.data.goods.status = 2
            $this.setData({
              banner: newBanner,
              moreSel: res.data,
              optionId: res.data.id,
              goods: $this.data.goods
            })
          } else {
            $this.data.goods.status = 1
            $this.setData({
              banner: newBanner,
              moreSel: res.data,
              optionId: res.data.id,
              goods: $this.data.goods
            })
          }
        }
      })
    },
  }
})